﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Labb3.View
{
    class SplitterParticle
    {
        int m_seed;
        Texture2D _splitterTexture;
        Random rand;
        Camera _camera;
        Vector2 _startPosition;
        Vector2 _splitterPosition;
        Vector2 _velocity;
        Vector2 _acceleration;
        float _diameter;

        public SplitterParticle(Vector2 startingPosition, int seed, Texture2D texture, Camera camera)
        {
            m_seed = seed;
            rand = new Random(seed);
            _camera = camera;
            _splitterTexture = texture;
            _diameter = 0.05f;
            _startPosition = startingPosition;
            _splitterPosition = new Vector2(_startPosition.X, _startPosition.Y);

            _velocity = new Vector2((float)(rand.NextDouble() * 2.0 - 1.0), (float)(rand.NextDouble() * 2.0 - 1.0));
            _velocity.Normalize();

            _acceleration = new Vector2(0.0f, 10f);

            float min = 1.5f;
            float max = 3.0f;
            //float min = 0.05f;
            //float max = 0.1f;
            float particleSpeed = min + ((float)(rand.NextDouble()) * (max - min));

            _acceleration *= particleSpeed;
            //System.Diagnostics.Debug.WriteLine(m_camera.GetVisualCoords(m_dogePosition));
        }

        internal void Update(GameTime gameTime)
        {
            //Vector2 randomDirection = new Vector2((float)rand.NextDouble() - 0.5f, (float)rand.NextDouble() - 0.5f);
            //randomDirection.Normalize();

            _splitterPosition += (_velocity / 500) * gameTime.ElapsedGameTime.Milliseconds / 15;
            _velocity += _acceleration * gameTime.ElapsedGameTime.Milliseconds / 5000;

            //System.Diagnostics.Debug.WriteLine(gameTime.ElapsedGameTime.Milliseconds);

            //m_dogePosition = m_dogePosition + m_velocity * gameTime.ElapsedGameTime.Milliseconds / 15;
            //m_velocity = m_velocity + m_acceleration * (gameTime.ElapsedGameTime.Milliseconds - 1000);


            //Vector2 newVelocity = new Vector2();
            //System.Diagnostics.Debug.WriteLine("doge Number: " + m_seed + "Position: " + m_dogePosition);
            //newVelocity.X = gameTime.ElapsedGameTime.Seconds * 

        }

        internal void Draw(SpriteBatch spritebatch)
        {
            int xCoord = (int)_camera.GetVisualCoords(_splitterPosition).X;
            int yCoord = (int)_camera.GetVisualCoords(_splitterPosition).Y;
            Vector2 size = _camera.GetVisualCoords(new Vector2(_diameter, _diameter));
            int width = (int)size.X;
            int height = (int)size.Y;

            //Rectangle dogeRectangle = new Rectangle((int)m_camera.GetVisualCoords(m_dogePosition).X, (int)m_camera.GetVisualCoords(m_dogePosition).Y, 114 / 5, 129 / 5);
            Rectangle splitterRectangle = new Rectangle(xCoord - (int)(size.X / 2), yCoord - (int)(size.Y / 2), width, height);
            spritebatch.Draw(_splitterTexture, splitterRectangle, Color.White);
            //spritebatch.Draw(_splitterTexture, splitterRectangle, null, Color.White, 0f, new Vector2(size.X / 2, size.Y / 2), SpriteEffects.None, 0f);
        }
    }
}
